package br.com.dev.tcc.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.Apolice;
import br.com.dev.tcc.bean.Veiculo;

public class ListaApolicesAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<Apolice> itens;

	public ListaApolicesAdapter(Context ctx, List<Apolice> itens) {

		inflater = LayoutInflater.from(ctx);
		this.itens = itens;
	}

	@Override
	public int getCount() {

		return itens.size();
	}

	@Override
	public Object getItem(int pos) {

		return itens.get(pos);
	}

	@Override
	public long getItemId(int pos) {

		return pos;
	}

	@Override
	public View getView(int pos, View view, ViewGroup vg) {

		final Apolice apolice = itens.get(pos);

		ViewHolder holder = null;

		if (view == null) {

			view = inflater.inflate(R.layout.apolice_item, null);

			holder = new ViewHolder();
			holder.tvApolice = (TextView) view.findViewById(R.id.tvValueApolice);
			holder.tvSegurado = (TextView) view.findViewById(R.id.tvSegurado);
			holder.tvVeiculo = (TextView) view.findViewById(R.id.tvVeiculo);

			view.setTag(holder);
		} 
		else
			holder = (ViewHolder) view.getTag();

		holder.tvApolice.setText(apolice.getHash());
		holder.tvSegurado.setText(apolice.getSegurado().getNome());
		Veiculo v = apolice.getVeiculo();
		holder.tvVeiculo.setText(v.getModelo() + ", " + v.getCor() + ", " + v.getPlaca());

		return view;
	}

	private class ViewHolder {

		public TextView tvApolice;
		public TextView tvSegurado;
		public TextView tvVeiculo;
	}
}
